package com.example.lin.day9_11;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;

import java.io.File;

import static android.support.v4.content.FileProvider.getUriForFile;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public static int NOTIFY_ID = 321;
    private static final String NOTIFY_CHANNEL_ID = "OK_CHANNEL_ID";
    private static final String NOTIFY_CHANNEL_NAME = "OK_CHANNEL_NAME";

    public void test1(View view) {
        //Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        //NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        if (manager != null) {
            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle("QQ消息通知")
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText("小丽,今晚哪里约!")
                    //在5.0之前的版本中,当通知刚到达时,在状态栏显示
                    .setTicker("QQ消息:小丽叫你今晚约!")
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID++, notification);
        }
    }


    public void test2(View view) {
        //Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        //NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        if (manager != null) {
            //创建 PendingIntent 跳转到 Activity
            Intent intent = new Intent(this, DetailActivity.class);
            intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤");
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle("QQ消息通知")
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText("小丽,今晚哪里约!")
                    //在5.0之前的版本中,当通知刚到达时,在状态栏显示
                    .setTicker("QQ消息:小丽叫你今晚约!")
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //添加意图对象
                    .setContentIntent(pendingIntent)

                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
//            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID++, notification);
        }
    }

    public void test3(View view) {
        //Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        //NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        if (manager != null) {
            String title = "QQ消息通知";
            String content = "小丽,今晚哪里约!";
            //创建 PendingIntent 跳转到 Activity
            Intent intent = new Intent(this, DetailActivity.class);
            intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤");
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Bitmap bigPic = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
            Bitmap bigLargeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.qq2);
            NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle()
                    //大图通知样式 的 大图标
                    .bigLargeIcon(bigLargeIcon)
                    //大图通知样式的 图片
                    .bigPicture(bigPic)
                    //通知的内容
                    .setSummaryText(content)
                    //通知的标题
                    .setBigContentTitle(title);

            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle(title)
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText(content)
                    //在5.0之前的版本中,当通知刚到达时,在状态栏显示
                    .setTicker(title + ":" + content)
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //添加意图对象
                    .setContentIntent(pendingIntent)

                    //设置 通知的样式(大图/长文本)
                    .setStyle(bigPictureStyle)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
//            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID++, notification);
        }
    }

    public void test4(View view) {
//Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        //NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        if (manager != null) {
            String title = "QQ消息通知";
            String content = "小丽,今晚哪里约!";
            //创建 PendingIntent 跳转到 Activity
            Intent intent = new Intent(this, DetailActivity.class);
            intent.putExtra("key", "阿里斯顿富欧式风dal;ksjf;阿里山叠加开发;阿里上看到肌肤");
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().setBigContentTitle(title).setSummaryText(content).bigText("asdjfhask阿里山的咖啡机阿斯利康打飞机阿里上看到肌肤啊拉克丝登记费爱上咖啡胸前语文哦发生了地方和阿里开始交电话费拉斯交电话费拉克丝交电话费");
            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle(title)
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText(content)
                    //在5.0之前的版本中,当通知刚到达时,在状态栏显示
                    .setTicker(title + ":" + content)
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //添加意图对象
                    .setContentIntent(pendingIntent)
                    //设置 通知的样式(大图/长文本)
                    .setStyle(bigTextStyle)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID, notification);
        }
    }

    public void test5(View view) {
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            //取消该应用程序发送的所有通知
//            manager.cancelAll();
            //取消指定ID 的通知
            manager.cancel(NOTIFY_ID);
        }
    }

    public void test6(View view) {


        new Thread(new Runnable() {
            @Override
            public void run() {
                showProgressNotification("碟中谍6.MP4", "正在连接中...", 0, true);
                SystemClock.sleep(3000);
                for (int i = 1; i <= 100; i++) {
                    showProgressNotification("碟中谍6.MP4", "下载中..." + i + "%", i, false);
                    SystemClock.sleep(100);
                }


                showCompleteNotification("path/碟中谍6.MP4", "下载完成!");
            }
        }).start();
    }

    /**
     *
     * @param fileName 通知标题
     * @param info 通知内容
     * @param progress 当前进度
     * @param indeterminate 是否是不明确的
     */
    private void showProgressNotification(String fileName, String info, int progress, boolean indeterminate) {
        //Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        //NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        if (manager != null) {
            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle(fileName)
                    //参数1是:总进度,参数2是:当前进度,参数3是:是否不明确
                    .setProgress(100, progress, indeterminate)
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText(info)
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID, notification);
        }
    }

    private static int NOTIFY_ID_COMPLETE = 99999;

    private void showCompleteNotification(String fileName, String info) {
        //Step1:获取通知管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        if (manager != null) {
            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle(fileName)
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText(info)
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID_COMPLETE, notification);
            manager.cancel(NOTIFY_ID);
        }
    }

    public void test7(View view) {
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        //在Android(O) 8.0 及以上,需要做以下配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (manager != null) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFY_CHANNEL_ID, NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(notificationChannel);
            }
        }
        if (manager != null) {

            //布局最大高度为 96dp
            RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.view_notification);
            remoteView.setTextViewText(R.id.tv_show, "拉克丝登记费老大撒框架");
            remoteView.setImageViewResource(R.id.iv_show, R.mipmap.dog);

//            布局最大高度为 256DP
            RemoteViews remoteViewBig = new RemoteViews(getPackageName(), R.layout.view_notification_big);
            remoteViewBig.setTextViewText(R.id.tv_show, "拉克丝登记费老大撒框架");
            remoteViewBig.setTextViewText(R.id.tv_show2, "这是 大的自定义View特有的TextView");
            remoteViewBig.setImageViewResource(R.id.iv_show, R.mipmap.dog);

            //点击事件,一般配合 BroadcastReceiver 使用
//            remoteView.setOnClickPendingIntent(R.id.iv_show,null);

            //Step3: 构造 Notification 对象
            Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANNEL_ID)
                    //设置通知的 小图标(必须设置,否则通知不显示)
                    .setSmallIcon(R.mipmap.qq2)
                    //通知标题
                    .setContentTitle("QQ消息通知")
                    //设置 通知 显示的时间
                    .setWhen(System.currentTimeMillis() - 3600000L)
                    //通知的内容
                    .setContentText("小丽,今晚哪里约!")
                    //在5.0之前的版本中,当通知刚到达时,在状态栏显示
                    .setTicker("QQ消息:小丽叫你今晚约!")
                    //设置普通布局,当通知栏空间不足的时候显示的布局
                    .setCustomContentView(remoteView)
                    //设置大布局,当通知栏空间足够的时候显示该布局
                    .setCustomBigContentView(remoteViewBig)
                    //设置 通知 来时的触发(铃声\震动\闪光)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //构造Notification对象
                    .build();

//            notification.flags |= Notification.FLAG_NO_CLEAR; //让通知不能滑动取消
//            notification.flags |= Notification.FLAG_INSISTENT; //重复发出 通知的声音/震动 直到用户点击该通知
//            notification.flags |= Notification.FLAG_ONGOING_EVENT; //让通知 显示 正在运行
            notification.flags |= Notification.FLAG_AUTO_CANCEL; //让通知被点击之后自动取消掉
//
            //Step2:唤醒通知, 每个 通知的ID 是唯一的.如果ID一样,则通知栏不会创建新的通知,会重用原来的通知
            manager.notify(NOTIFY_ID++, notification);
        }


    }



}
